package com.yky.action.base;

import java.io.PrintWriter;
import java.text.ParseException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONArray;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;

/**
 * 基础Action，所有的Action都可以继承，省去写page和控制层统一处理，继承后，只要些Results和execute方法
 * 
 */
@SuppressWarnings("serial")
public class BaseActionSupport extends ActionSupport {

	// protected ContagionService service = null;

	/** jsp页面传过来的当前页面 * */
	protected int page = 1;

	public int getPage() {
		return page;
	}

	public void setPage(int page) {
		this.page = page;
	}
	
	public HttpSession getSession() {
		return getRequest().getSession(false);
	}
	
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	// protected ContagionService getContagionServiceInstance() {
	// this.initConfigPath();
	// service = ContagionServiceImpl.getInstance();
	// return service;
	// }

	// protected void initContagionServiceInstance() {
	// this.getContagionServiceInstance();
	// }

	// private void initConfigPath() {
	// ActionContext ac = ActionContext.getContext();
	// ServletContext sc = (ServletContext) ac
	// .get(ServletActionContext.SERVLET_CONTEXT);
	// StaticVariable.realPath = sc.getRealPath("/")
	// + "WEB-INF/contagion_config.xml";
	// }

	protected void pringWriterToPage(String content) throws Exception {
		this.pringWriterToPage(content, null);
	}

	protected void pringWriterToPage(String content, String contentType)
			throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		// "application/json"
		// ".txt"="text/plain"
		// "text/xml"
		if (contentType != null && !"".equals(contentType)) {
			response.setContentType(contentType);
		} else {
			// response.setContentType("text/plain");
		}
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter pw = response.getWriter();
		pw.print(content);
		pw.flush();
		pw.close();
	}

	protected String formatToJson(Object obj) throws ParseException {
		JSONArray jsonObject = JSONArray.fromObject(obj);
		try {
			// System.out.println(jsonObject.toString());
			return jsonObject.toString();
		} catch (Exception e) {
			return "";
		}
	}

	/**
	 * 获取IP地址
	 * 
	 * @param request
	 * @return
	 */
	protected String getIpAddr() {
		String ip = ServletActionContext.getRequest().getHeader(
				"x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = ServletActionContext.getRequest().getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = ServletActionContext.getRequest().getHeader(
					"WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = ServletActionContext.getRequest().getRemoteAddr();
		}
		return ip;
	}

	protected void printWriterJsonToPage(String content) throws Exception {
		this.printWriterToPage(content, "application/json");
	}

	protected void printWriterToPage(String content, String contentType)
			throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		// "application/json"
		// ".txt"="text/plain"
		// "text/xml"
		if (contentType != null && !"".equals(contentType)) {
			response.setContentType(contentType);
		} else {
			response.setContentType("text/plain");
		}
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Prama", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		PrintWriter pw = response.getWriter();
		pw.print(content);
		pw.flush();
		pw.close();
	}

}
